package com.example.android_wx.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.widget.ContentLoadingProgressBar;

import com.example.android_wx.R;
import com.example.android_wx.base.BaseActivity;

public class WebActivity extends BaseActivity {
    private WebView mainWebView;
    private AppCompatImageView aselinImafeView;
    private AppCompatImageView forwardImafeView;
    private AppCompatImageView refreshImafeView;
    private AppCompatEditText urlEditText;
    private AppCompatImageView sendImafeView;
    private ContentLoadingProgressBar mainProgressBar;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_web);
        super.onCreate(savedInstanceState);

    }

    @Override
    public void initView() {
        mainWebView = findViewById(R.id.mainWebView);
        aselinImafeView = findViewById(R.id.aselinImafeView);
        forwardImafeView = findViewById(R.id.forwardImafeView);
        refreshImafeView = findViewById(R.id.refreshImafeView);
        urlEditText = findViewById(R.id.urlEditText);
        sendImafeView = findViewById(R.id.sendImafeView);
        mainProgressBar = findViewById(R.id.mainProgressBar);
        mainProgressBar.setMax(100);

        mainWebView.loadUrl("https://www.baidu.com/");
        WebSettings webSettings = mainWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);//开启javascipt支持
        webSettings.setUseWideViewPort(true);//开启界面的缩放
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);//设置缓存方式
        webSettings.setAllowFileAccess(true);//设置是否允许webview访问文件
        webSettings.setBlockNetworkImage(false);//屏蔽图片


    }

    @Override
    public void initEvent() {
        aselinImafeView.setOnClickListener(v -> {
            if (mainWebView.canGoBack()) mainWebView.goBack();
        });

        forwardImafeView.setOnClickListener(v -> {
            if (mainWebView.canGoForward()) mainWebView.goForward();
        });

        refreshImafeView.setOnClickListener(v -> {
            mainWebView.reload();
        });

        sendImafeView.setOnClickListener(v -> {
            String url = urlEditText.getText().toString();
            if (url.isEmpty()) return;
            System.out.println(url);
            if (url.contains("https://") || url.contains("http://")) {
                mainWebView.loadUrl(url);

            }
            mainWebView.loadUrl("https://" + url);

        });


        mainWebView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

                mainWebView.loadUrl(request.getUrl().toString());
                return true;
            }
        });

        mainWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                System.out.println(newProgress);
                if (newProgress < 100) mainProgressBar.setVisibility(View.VISIBLE);
                if (newProgress >= 100) mainProgressBar.setVisibility(View.GONE);
                mainProgressBar.setProgress(newProgress);
            }
        });


    }
}
